package org.moflon.boardgameplatform.view;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.moflon.boardgameplatform.controller.Controller;

public class Field implements SelectionListener {

	public static final int size = 60;
	
	private Controller controller;
	private Point pos;
	private boolean hasFigure;
	private Button button;

	public Field(int i, int j, Controller controller, Composite parent, int style, Color color) {
		this.controller = controller;
		pos = new Point(i, j);
		hasFigure = false;
		
		button = new Button(parent, style);
		button.setBackground(color);
		GridData gd_btnNewButton = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
		gd_btnNewButton.widthHint = size;
		gd_btnNewButton.heightHint = size;
		button.setLayoutData(gd_btnNewButton);
		button.addSelectionListener(this);
		
		clear();
	}

	public void setFigure(String name) {
		hasFigure = true;
		Font x = new Font(Display.getCurrent(), "Arial", 36, SWT.BOLD);
		button.setFont(x);
		button.setText(name);
		//button.setImage(SWTResourceManager.getImage("./resources/ghost.png"));
	}

	public void clear() {
		hasFigure = false;
		button.setText("");
		//button.setImage(SWTResourceManager.getImage("./resources/empty.png"));
	}
	
	@Override
	public void widgetSelected(SelectionEvent e) {
		controller.fieldPressed(this);
	}

	@Override
	public void widgetDefaultSelected(SelectionEvent e) {
	}

	public Point getPos() {
		return pos;
	}
	
	public boolean occupied(){
		return hasFigure;
	}
}
